package com.all.play.mypsm.shoppingtcart.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.all.play.mypsm.R;
import com.all.play.mypsm.home.bean.GoodsInfo;
import com.all.play.mypsm.shoppingtcart.utils.CartSorage;
import com.all.play.mypsm.shoppingtcart.view.NumberSubAddView;
import com.all.play.mypsm.utils.Constants;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/2.
 */

public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.MyViewHolder> {

    private Context mContext;
    private List<GoodsInfo> allData;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private CheckBox cbAll;

    public ShoppingCartAdapter(Context mContext, List<GoodsInfo> allData, CheckBox checkboxAll, TextView tvShopcartTotal, CheckBox cbAll) {

        this.mContext = mContext;
        this.allData = allData;
        this.checkboxAll = checkboxAll;
        this.tvShopcartTotal = tvShopcartTotal;
        this.cbAll = cbAll;

        //设置总价格
        setTotalPrices();

        //条目监听
        setItemClick();

        //列表出现,就校验
        checkAll();


    }

    /**
     *条目点击,本类当中回传
     *@author 聪明一只哈
     *@time 2018/4/2
     */
    private void setItemClick() {
        //条目选中状态
        setOnItemClickesListerner(new OnItemClickesListerner() {
            @Override
            public void itemOnclick(int position) {

                //1.根据位置,获得bean对象
                GoodsInfo goodsInfo = allData.get(position);

                //2.check状态要取反
                goodsInfo.setSelected(!goodsInfo.isSelected());

                //3.点击的当前条目要刷新
                notifyItemChanged(position);

                //3.3设置全选与非全选状态  校验是否全选
                checkAll();


                //4.计算总价
                setTotalPrices();

            }
        });


        //checkbox监听全选状态
        /*checkboxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });*/
        checkboxAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //-----------------编辑状态
                //1.得到状态
                boolean checked = checkboxAll.isChecked();
                //2.根据状态设置条目全选或者非全选
                //setItemCheckedState(checked);
                checkAll_none(checked);
                //3.计算总价
                setTotalPrices();

            }
        });
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //完成状态
                //1.得到状态
                boolean checked = cbAll.isChecked();
                //2.根据状态设置条目全选或者非全选
                //setItemCheckedState(checked);
                checkAll_none(checked);
                //3.计算总价
                //setTotalPrices();

            }
        });
    }



    /**
     *根据条目状态选择
     *@author 聪明一只哈
     *@time 2018/4/2 17:08
     */
    public void checkAll_none(boolean checked) {

        if (allData!=null&&allData.size()>0){
            for (int i=0;i<allData.size();i++){
                GoodsInfo goodsInfo = allData.get(i);
                goodsInfo.setSelected(checked);
                //数据刷新
                notifyItemChanged(i);
            }


        }
    }


    /**
     *全选按钮checkbox校验是否全选
     *@author 聪明一只哈
     *@time 2018/4/2 16:56
     */
    public void checkAll() {
        if (allData!=null&&allData.size()>0){
            int position=0;
            for (int i=0;i<allData.size();i++){
                GoodsInfo goodsInfo = allData.get(i);
                if (!goodsInfo.isSelected()){
                    //只要有一个是非选中状态,则是没有全选
                    checkboxAll.setChecked(false);
                    cbAll.setChecked(false);
                }else {
                    position++;
                    cbAll.setChecked(true);
                }
            }

            if (position==allData.size()){
                checkboxAll.setChecked(true);
            }

            //并且需要一加载列表,就要校验

        }else{
            //默认状态
            checkboxAll.setChecked(false);
            cbAll.setChecked(false);
        }



    }

    /**
     * 设置总价格
     *
     * @author 聪明一只哈
     * @time 2018/4/2 16:11
     */
    public void setTotalPrices() {
        tvShopcartTotal.setText("￥"+getnumPduprice() + "元");
    }

    /**
     *价格改变
     *@author 聪明一只哈
     *@time 2018/4/2 16:26
     */
    public double getnumPduprice() {
        double total = 0.0;
        for (int i = 0; i < allData.size(); i++) {
            GoodsInfo goodsInfo = allData.get(i);
            if (goodsInfo.isSelected()) {
                total = total + Double.valueOf(goodsInfo.getNumber()) * Double.valueOf(goodsInfo.getCover_price());
            }
        }

        return total;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(View.inflate(mContext, R.layout.item_shop_cart, null));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final GoodsInfo goodsInfo = allData.get(position);
        if (goodsInfo != null) {

            holder.cb_gov.setChecked(goodsInfo.isSelected());

            Glide.with(mContext).load(Constants.BASE_URl_IMAGE + goodsInfo.getFigure()).into(holder.iv_gov);

            holder.tv_desc_gov.setText(goodsInfo.getName());
            holder.tv_price_gov.setText("￥" + goodsInfo.getCover_price() + "元");

            holder.numberSubAddView.setValue(goodsInfo.getNumber());

            //设置数量增加和减少的监听
            holder.numberSubAddView.setOnNumberChangeListener(new NumberSubAddView.OntvNumberChangeListener() {
                @Override
                public void onChange(int value) {
                    //1.value,改变,则内存中的数据需要改变
                    goodsInfo.setNumber(value);
                    //2.内存缓存中数据更新,保存本地
                    CartSorage.getInstance().updateData(goodsInfo);
                    //3.刷新当前条目
                    notifyItemChanged(position);
                    //4.改变总价格
                    setTotalPrices();


                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return allData == null ? 0 : allData.size();
    }

    /**
     *删除选中的条目
     *@author 聪明一只哈
     *@time 2018/4/2 17:47
     */
    public void deleteItem() {

        if(allData!=null&&allData.size()>0){
            for (int i = 0; i < allData.size(); i++) {
                GoodsInfo goodsInfo = allData.get(i);
                if (goodsInfo.isSelected()){
                    //内存移除
                    allData.remove(goodsInfo);
                    //本地保存
                    CartSorage.getInstance().deleteData(goodsInfo);
                    //adapter刷新
                    notifyItemChanged(i);
                    //集合减少1后,总大小减1
                    i--;

                }

            }
        }





    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private CheckBox cb_gov;
        private ImageView iv_gov;
        private TextView tv_desc_gov;
        private TextView tv_price_gov;
        private NumberSubAddView numberSubAddView;

        public MyViewHolder(View itemView) {
            super(itemView);
            cb_gov = itemView.findViewById(R.id.cb_gov);
            iv_gov = itemView.findViewById(R.id.iv_gov);
            tv_desc_gov = itemView.findViewById(R.id.tv_desc_gov);
            tv_price_gov = itemView.findViewById(R.id.tv_price_gov);
            numberSubAddView = itemView.findViewById(R.id.numberAddSubView);

            //设置条目点击
           itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   //条目点击的时候,触发OnItemClickesListerner接口
                    if (onItemClickesListerner!=null){
                        onItemClickesListerner.itemOnclick(getLayoutPosition());
                    }
               }
           });

        }
    }

    public interface OnItemClickesListerner{
        void itemOnclick(int position);
    }

    private OnItemClickesListerner onItemClickesListerner;

    public void setOnItemClickesListerner(OnItemClickesListerner onItemClickesListerner) {
        this.onItemClickesListerner = onItemClickesListerner;
    }
}
